package pe.edu.pucp.cpnc.mm.bean;

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement
public class Building extends Object{
	
    private int hp;
    private int def;
    @XmlElementWrapper
    @XmlElement(name ="entrance")
    private ArrayList<Entrance> entrances;

    public int getHp() {
            return hp;
    }
    public void setHp(int hp) {
            this.hp = hp;
    }
    public int getDef() {
            return def;
    }
    public void setDef(int def) {
            this.def = def;
    }

    public Building(){

    }

    @Override
    public int add(int posX, int posY, Object object, Map map , int idPlayer){
            return add(posX, posY, object, map, idPlayer, 0);
    }

    @Override
    public int add(int posX, int posY, Object object, Map map , int idPlayer, int orientation){

        Building b = (Building)object;
        int sX = b.getSizeX(); //Width Limit
        int sY = b.getSizeY(); //Height Limit

        if(orientation == 1 || orientation == 3){
            int aux = sX;
            sX = sY;
            sY = aux;
        }

        for(int i=posY ;i<posY + sY; i++){
            for(int j=posX;j<posX + sX;j++){
                if(!(i>=0 && i<map.getSizeY() && j>=0 && j<map.getSizeX())){
                    System.out.println("El objeto esta fuera de los limites.");
                    return -1;
                }
                if(!(b.getPath() ==  map.getTiles()[i][j].getTile().getPath())){
                    System.out.println("El objeto no puede atravesar este tile.");
                    return -1;
                }
                if (map.getTiles()[i][j].getOccupied()==1){
                    System.out.println("El tile esta ocupado.");
                    return -1;
                }				
            }
        }

        ObjectDetail objDetail = new ObjectDetail();

        objDetail.setObject(b);
        objDetail.setPosX(posX);
        objDetail.setPosY(posY);
        objDetail.setOrientation(orientation);

        map.getPlayers().get(idPlayer).getObjectDetails().add(objDetail);

        for(int i=posY;i<posY + sY;i++){ //Set tile Occupied
            for(int j=posX;j<sX + posX;j++){
                map.getTiles()[i][j].setOccupied(1);
                map.getTiles()[i][j].setObjInside(objDetail);
            }
        }
        map.getTiles()[posY][posX].setIsRoot(1);
        return 0;		
    }

    /**
     * @return the entrances
     */
    @XmlTransient
    public ArrayList<Entrance> getEntrances() {
        return new ArrayList<>(entrances);
    }

    /**
     * @param entrances the entrances to set
     */
    public void setEntrances(ArrayList<Entrance> entrances) {
        this.entrances = entrances;
    }
	
}

	